package com.longxi.tianjintianbao.ui.presenter.quality;

import android.content.Intent;
import android.text.TextUtils;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.Personnel;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.quality.IQualityStateChangesAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.TimeComparisonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class QualityStateChangesAtPresenter extends BasePresenter<IQualityStateChangesAtView> {

    public List<Personnel> personnels = new ArrayList<>();//存放员工的数据 用于多选

    public QualityStateChangesAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        if(mContext.getIntent().getStringExtra(AppConst.STATE).equals("1")){
            mContext.setTitle("品质检查结束");
        }else if(mContext.getIntent().getStringExtra(AppConst.STATE).equals("2")){
            mContext.setTitle("品质检查生成整改");
        }
        mContext.setTvLeft(MyUtils.getString(R.string.back));
    }

    public void goFinshOrChange() {
        if(mContext.getIntent().getStringExtra(AppConst.STATE).equals("1")){
            saveQualityState("1");
        }else if(mContext.getIntent().getStringExtra(AppConst.STATE).equals("2")){
            saveQualityState("2");
        }
    }

    public void saveQualityState(String button) {
        String finishremark = getView().getEtFinishremark().getText().toString().trim();
        String lastdate = getView().getTvLastdate().getText().toString().trim();

        if(personnels.size() == 0){
            MyUtils.showToast("请选择整改负责人");
            return;
        }

        if(TextUtils.isEmpty(lastdate)){
            MyUtils.showToast("请选择需要整改完成的最后时间");
            return;
        }

        if(TextUtils.isEmpty(finishremark)){
            MyUtils.showToast("请输入完成说明");
            return;
        }

        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", mContext.getIntent().getStringExtra(AppConst.BILLID));
            object.put("personid", personnels.get(0).getUserid());
            object.put("build", mContext.getIntent().getStringExtra(AppConst.BUILD));
            object.put("buildid", mContext.getIntent().getStringExtra(AppConst.BUILDID));
            object.put("lastdate", TimeComparisonUtils.getDate());
            object.put("finishremark", finishremark);
            object.put("button", button);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().saveQualityState(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == AppConst.IntentRequstCode.ACTIVITY_PERSONNEL) {
            if(data != null){
                personnels = (List<Personnel>) data.getSerializableExtra(AppConst.PERSONNEL_LISTINTENT);
                getView().getTvPerson().setText(personnels.get(0).getUsername());
            }
        }
    }

    @Override
    public void detachView() {
        super.detachView();
        this.personnels = null;
    }
}
